import Vue from 'vue';
import Store from 'src/store';
import {userMessage} from "src/lib/util/message";
import {i18n} from "boot/i18n";

const Guild = {
  info: async (id) => {
    let {data} = await Vue.prototype.$axios.get('/api/guild/info', {
      params: {
        id
      }
    });
    if (data.result) {
      return data.data;
    } else {
      userMessage('error',  i18n.t(data.error) || '发生了一些错误，请稍后再试。');
    }
    return false;
  },
  list: async () => {
    let {data} = await Vue.prototype.$axios.post('api/guild/member/list', {
      username: Store.state.account.username,
      token: Store.state.account.token
    });
    if (data.result) {
      return data.data;
    } else {
      userMessage('error',  i18n.t(data.error) || '发生了一些错误，请稍后再试。');
    }
    return false;
  },
  memberBox: async (member) => {
    let {data} = await Vue.prototype.$axios.post('/api/guild/member/box', {
      username: Store.state.account.username,
      token: Store.state.account.token,
      member
    });
    if (data.result) {
      return data.data;
    } else {
      userMessage('error',  i18n.t(data.error) || '发生了一些错误，请稍后再试。');
    }
    return false;
  },
  memberInfo: async (member) => {
    let {data} = await Vue.prototype.$axios.post('/api/guild/member/info', {
      username: Store.state.account.username,
      token: Store.state.account.token,
      member
    });
    if (data.result) {
      return data.data;
    } else {
      userMessage('error',  i18n.t(data.error) || '发生了一些错误，请稍后再试。');
    }
    return false;
  },
  teamList: async () => {
    let {data} = await Vue.prototype.$axios.post('/api/guild/team/list', {
      username: Store.state.account.username,
      token: Store.state.account.token
    });
    if (data.result) {
      return data.data;
    } else {
      userMessage('error',  i18n.t(data.error) || '发生了一些错误，请稍后再试。');
    }
    return false;
  },
  uploadTeam: async (team) => {
    let {data} = await Vue.prototype.$axios.post('/api/guild/team/upload', {
      username: Store.state.account.username,
      token: Store.state.account.token,
      team
    });
    if (data.result) {
      return data.data;
    } else {
      userMessage('error',  i18n.t(data.error) || '发生了一些错误，请稍后再试。');
    }
    return false;
  },
  deleteTeam: async (id) => {
    let {data} = await Vue.prototype.$axios.post('/api/guild/team/remove', {
      username: Store.state.account.username,
      token: Store.state.account.token,
      id
    });
    if (data.result) {
      return data.data;
    } else {
      userMessage('error',  i18n.t(data.error) || '发生了一些错误，请稍后再试。');
    }
    return false;
  }
}

export default Guild;
